package com.lms.service.impl;

import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.lms.entity.Dormbuildbaseinfo;
import com.lms.mapper.DormbuildbaseinfoMapper;
import com.lms.service.DormbuildbaseinfoService;
import com.lms.vo.DataVo;
import org.apache.poi.ss.usermodel.Row;
import org.apache.poi.ss.usermodel.Sheet;
import org.apache.poi.xssf.usermodel.XSSFWorkbook;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.web.multipart.MultipartFile;

import java.io.IOException;
import java.io.InputStream;
import java.util.ArrayList;
import java.util.List;

/**
 * @Description: 宿舍楼宇后勤基础数据业务逻辑层接口实现类
 * @author: L-B-S
 * @Date: 2021/3/8 9:09
 * @modified_By:
 * @Version: 0.0$
 */
@Service
public class DormbuildbaseinfoServiceImpl implements DormbuildbaseinfoService {

    @Autowired
    private DormbuildbaseinfoMapper dormbuildbaseinfoMapper;

    @Override
    public DataVo<Dormbuildbaseinfo> getList(Integer page, Integer limit) {
        DataVo<Dormbuildbaseinfo> dormbuildbaseinfoDataVo = new DataVo<>();

        dormbuildbaseinfoDataVo.setCode(0);
        dormbuildbaseinfoDataVo.setMsg("");
        IPage<Dormbuildbaseinfo> dormbuildbaseinfoPage = new Page<>(page, limit);
        IPage<Dormbuildbaseinfo> result = dormbuildbaseinfoMapper.selectPage(dormbuildbaseinfoPage, null);

        dormbuildbaseinfoDataVo.setCount(result.getTotal());
        dormbuildbaseinfoDataVo.setData(result.getRecords());

        return dormbuildbaseinfoDataVo;
    }

    @Override
    public DataVo<Dormbuildbaseinfo> delDbbis(List<Dormbuildbaseinfo> dbbiList) {
        DataVo<Dormbuildbaseinfo> dataVo = new DataVo<>();
        List<Long> ids = new ArrayList<>();
        if (dbbiList.size() == 0) {
            dataVo.setCode(1);
            dataVo.setMsg("您没有选择删除的行，请选择后操作");
        } else {
            for (Dormbuildbaseinfo dormbuildbaseinfo : dbbiList) {
                ids.add(dormbuildbaseinfo.getId());
            }
            dormbuildbaseinfoMapper.deleteBatchIds(ids);
            dataVo.setCode(0);
            dataVo.setMsg("删除成功，正在刷新数据，请稍等");
        }
        return dataVo;
    }

    @Override
    public DataVo<Dormbuildbaseinfo> delOne(Dormbuildbaseinfo dormbuildbaseinfo) {
        DataVo<Dormbuildbaseinfo> dataVo = new DataVo<>();
        int i = dormbuildbaseinfoMapper.deleteById(dormbuildbaseinfo.getId());
        if (i > 0) {
            dataVo.setCode(0);
            dataVo.setMsg("删除行成功");
        } else {
            dataVo.setCode(1);
            dataVo.setMsg("网络异常，删除失败");
        }
        return dataVo;
    }

    @Override
    public DataVo<Dormbuildbaseinfo> addOne(Dormbuildbaseinfo dormbuildbaseinfo) {
        DataVo<Dormbuildbaseinfo> dataVo = new DataVo<>();
        Dormbuildbaseinfo dbbi = dormbuildbaseinfoMapper.selectByDormBuild(dormbuildbaseinfo.getDormBuild());
        if (dbbi != null) {
            dataVo.setCode(1);
            dataVo.setMsg("此编号的宿舍楼宇已存在，请修改后再试");
        } else {
            dormbuildbaseinfoMapper.insert(dormbuildbaseinfo);
            dataVo.setCode(0);
            dataVo.setMsg("新增数据成功，正在刷新数据，请稍等...");
        }
        return dataVo;
    }

    @Override
    public DataVo<Dormbuildbaseinfo> edit(Dormbuildbaseinfo dormbuildbaseinfo) {
        DataVo<Dormbuildbaseinfo> dataVo = new DataVo<>();
        Dormbuildbaseinfo obj = dormbuildbaseinfoMapper.selectById(dormbuildbaseinfo.getId());
        // 什么也没有修改就提交，不能成功
        if (obj.equals(dormbuildbaseinfo)) {
            dataVo.setCode(1);
            dataVo.setMsg("您还未修改任何一条属性，请修改后提交");
        } else {
            dormbuildbaseinfoMapper.updateById(dormbuildbaseinfo);
            dataVo.setCode(0);
            dataVo.setMsg("修改成功，正在刷新页面，请稍等...");
        }
        return dataVo;
    }

    @Override
    public DataVo<Dormbuildbaseinfo> uploadExcel(MultipartFile file) {
        DataVo<Dormbuildbaseinfo> dataVo = new DataVo<>();
        try {
            String filename = file.getOriginalFilename();
            System.out.println(filename);
            InputStream inputStream = file.getInputStream();
            final XSSFWorkbook workbook = new XSSFWorkbook(inputStream);
            Sheet sheet = workbook.getSheetAt(0);
            Row headerRow = sheet.getRow(0);
            if ("校区".equals(headerRow.getCell(0).getStringCellValue()) && "宿舍楼宇".equals(headerRow.getCell(1).getStringCellValue()) && "面积".equals(headerRow.getCell(2).getStringCellValue()) && "建设时间".equals(headerRow.getCell(3).getStringCellValue()) && "投用时间".equals(headerRow.getCell(4).getStringCellValue()) && "房屋结构".equals(headerRow.getCell(5).getStringCellValue()) && "屋面面积（㎡）".equals(headerRow.getCell(6).getStringCellValue()) && "外立面材质".equals(headerRow.getCell(7).getStringCellValue()) && "房间数（间）".equals(headerRow.getCell(8).getStringCellValue()) && "住宿人数（人）".equals(headerRow.getCell(9).getStringCellValue()) && "配电箱（个）".equals(headerRow.getCell(10).getStringCellValue()) && "卫生间（间）".equals(headerRow.getCell(11).getStringCellValue()) && "消防栓（个）".equals(headerRow.getCell(12).getStringCellValue()) && "暖气井（个）".equals(headerRow.getCell(13).getStringCellValue()) && "电梯（部）".equals(headerRow.getCell(14).getStringCellValue())) {
                int index = 1;
                boolean flag = true;
                List<Dormbuildbaseinfo> dbbiList = new ArrayList<>();
                while (flag) {
                    Row row = sheet.getRow(index++);
                    if (row != null) {
                        Dormbuildbaseinfo dormbuildbaseinfo = new Dormbuildbaseinfo();
                        dormbuildbaseinfo.setLocation(String.valueOf(row.getCell(0)));
                        dormbuildbaseinfo.setDormBuild(String.valueOf(row.getCell(1)).split("\\.")[0]);
                        dormbuildbaseinfo.setArea(Double.parseDouble(row.getCell(2).toString()));
                        dormbuildbaseinfo.setBuildTime((int) row.getCell(3).getNumericCellValue());
                        dormbuildbaseinfo.setUseTime((int) row.getCell(4).getNumericCellValue());
                        dormbuildbaseinfo.setBuildStru(String.valueOf(row.getCell(5)));
                        dormbuildbaseinfo.setRoofArea(row.getCell(6).getNumericCellValue());
                        dormbuildbaseinfo.setFacadeMaterial(String.valueOf(row.getCell(7)));
                        dormbuildbaseinfo.setRoomNum((int) row.getCell(8).getNumericCellValue());
                        dormbuildbaseinfo.setDormStuNum((int) row.getCell(9).getNumericCellValue());
                        dormbuildbaseinfo.setSwitchBoxNum((int) row.getCell(10).getNumericCellValue());
                        dormbuildbaseinfo.setToiletNum((int) row.getCell(11).getNumericCellValue());
                        dormbuildbaseinfo.setFireHydrantNum((int) row.getCell(12).getNumericCellValue());
                        dormbuildbaseinfo.setHeatWellNum((int) row.getCell(13).getNumericCellValue());
                        dormbuildbaseinfo.setElevatorNum((int) row.getCell(14).getNumericCellValue());
                        dbbiList.add(dormbuildbaseinfo);
                    } else {
                        flag = false;
                    }
                }
                inputStream.close();
                int count = dbbiList.size();
                if (count > 0) {
                    for (Dormbuildbaseinfo dormbuildbaseinfo : dbbiList) {
                        if (dormbuildbaseinfoMapper.selectByDormBuild(dormbuildbaseinfo.getDormBuild()) != null) {
                            continue;
                        }
                        dormbuildbaseinfoMapper.insert(dormbuildbaseinfo);
                        count--;
                    }
                    if (count == 0) {
                        dataVo.setCode(0);
                        dataVo.setMsg("文件上传成功，正在刷新数据，请稍等...");
                    } else {
                        dataVo.setCode(1);
                        dataVo.setMsg("文件中有无效数据(请及时过滤)，有效数据已上传");
                    }
                } else {
                    dataVo.setCode(2);
                    System.out.println("文件中不存在数据，请核实后再上传");
                }
            } else {
                dataVo.setCode(3);
                dataVo.setMsg("表格表头不规范，请严格按照规范声明表头");
            }
        } catch (IOException e) {
            e.printStackTrace();
        }
        return dataVo;
    }
}
